package com.wtx.chapter06;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

/**
 * @description: Binary search tree, 二分搜索树
 * @author: wtx
 * @createDate: 2020/5/21
 */
//内容必须具有可比较性
public class BST<E extends Comparable<E>> {

    private Node root;
    private int size;

    private class Node{

        public E e;
        public Node left,right;

        public Node(E e) {
            this.e = e;
            this.left = null;
            this.right = null;
        }
    }

    public BST() {
        this.root = null;
        this.size = 0;
    }

    public int getSize(){
        return this.size;
    }

    public boolean isEmpty(){
        return size==0;
    }

    public void add(E e){
        root = add(root,e);
    }

    //改进
    private Node add(Node node,E e){
        if (node == null){
            size++;
            return new Node(e);
        }
        if (e.compareTo(node.e) > 0 )
            node.right = add(node.right,e);
        else if (e.compareTo(node.e) < 0)  //不能使用else，因为没有 e==node.e 的判断
            node.left = add(node.left,e);
        return node;
    }

    //查询
    public boolean contains(E e){

        return contains(root,e);
    }
    private boolean contains(Node node, E e){

        if (node == null)
            return false;

        if (e.compareTo(node.e) == 0)
            return true;
        else if (e.compareTo(node.e) < 0)
            return contains(node.left,e);
        else
            return contains(node.right,e);
    }

    //前序遍历
    public void preOrder(){
        preOrder(root);
    }
    private void preOrder(Node node){

        if (node == null)
            return;

        System.out.println(node.e);
        preOrder(node.left);
        preOrder(node.right);
    }

    //前序的非递归实现
    public void preOrderNR(){

        Stack<Node> stack = new Stack();
        stack.push(root);
        while (!stack.isEmpty()){
            Node cur = stack.pop();
            System.out.println(cur.e);
            System.out.println(cur.e);

            if (cur.right != null)
                stack.push(cur.right);
            if (cur.left != null)
                stack.push(cur.left);
        }
    }

    //中序遍历
    public void inOrder(){
        inOrder(root);
    }
    private void inOrder(Node node){

        if (node == null)
            return;

        inOrder(node.left);
        System.out.println(node.e);
        inOrder(node.right);
    }

    //后序遍历
    public void postOrder(){
        postOrder(root);
    }
    private void postOrder(Node node){

        if (node == null)
            return;
        postOrder(node.left);
        postOrder(node.right);
        System.out.println(node.e);
    }

    //层序遍历
    public void levelOrder(){

        Queue<Node> q = new LinkedList<>();
        q.add(root);
        while (!q.isEmpty()){
            Node cur = q.remove();
            System.out.println(cur.e);

            if (cur.left!=null)
                q.add(cur.left);
            if (cur.right!=null)
                q.add(cur.right);
        }
    }

    // 寻找二分搜索树的最小元素
    public E minimum(){

        if (size==0)
            throw new IllegalArgumentException("Binary search tree is empty");

        Node miniNode = minimum(root);
        return miniNode.e;
    }

    // 返回以node为根的二分搜索树的最小值所在的节点
    private Node minimum(Node node){
        if ( node.left == null )
            return node;
        return minimum(node.left);
    }

    // 寻找二分搜索树的最大元素
    public E maximum(){
        if (size==0)
            throw new IllegalArgumentException("BST is empty");
        return maximum(root).e;
    }


    // 返回以node为根的二分搜索树的最大值所在的节点
    private Node maximum(Node node){

        if (node.right == null)
            return node;
        return maximum(node.right);
    }

    // 从二分搜索树中删除最小值所在节点, 返回最小值
    public E removeMin(){
        E ret = minimum();
        //删除最小结点之后的新根结点
        root = removeMin(root);
        return ret;
    }
    // 删除掉以node为根的二分搜索树中的最小节点
    //1、如果没有左子树，说明该结点为最小结点，删除，并用该结点的右子树接上
    //2、如果有左子树，递归调用，删除左子树的最小结点
    private Node removeMin(Node node){

        if( node.left == null ){
            Node rightNode = node.right;
            node.right = null;
            size--;
            return rightNode;
        }

        node.left = removeMin(node.left);
        return node;
    }

    // 从二分搜索树中删除最大值所在节点
    public E removeMax(){
        E ret = maximum();
        root = removeMax(root);
        return ret;
    }

    // 删除掉以node为根的二分搜索树中的最大节点
    // 返回删除节点后新的二分搜索树的根
    private Node removeMax(Node node){

        if( node.right == null){

            Node leftNode = node.left;
            node.left = null;
            size--;
            return leftNode;
        }
        node.right = removeMax(node.right);
        return node;
    }

    //删除指定结点
    public void remove(E e){

        root = remove(root,e);
    }
    private Node remove(Node node,E e){

        if (node == null)
            return null;

        if (e.compareTo(node.e)<0){
            node.left = remove(node.left, e);
            return node;
        }else if (e.compareTo(node.e)>0){
            node.right = remove(node.right,e);
            return node;
        }else { //e == node.e

            if (node.left == null){
                Node rightNode = node.right;
                node.right = null;
                size--;
                return rightNode;
            }else if (node.right == null){
                Node leftNode = node.left;
                node.left = null;
                size--;
                return leftNode;
            }  //左右子树都不为空

            //找待删除结点的右子树的最大结点
            //用它来取代待删除结点的位置
            Node successor = minimum(node.right);
            successor.right = removeMin(node.right);
            successor.left = node.left;
            node.left = node.right = null;
            return successor;
        }
    }

    @Override
    public String toString(){
        StringBuilder res = new StringBuilder();
        generateBSTString(root, 0, res);
        return res.toString();
    }

    // 生成以node为根节点，深度为depth的描述二叉树的字符串
    private void generateBSTString(Node node, int depth, StringBuilder res){

        if(node == null){
            res.append(generateDepthString(depth) + "null\n");
            return;
        }

        res.append(generateDepthString(depth) + node.e + "\n");
        generateBSTString(node.left, depth + 1, res);
        generateBSTString(node.right, depth + 1, res);
    }

    private String generateDepthString(int depth){
        StringBuilder res = new StringBuilder();
        for(int i = 0 ; i < depth ; i ++)
            res.append("--");
        return res.toString();
    }


    //插入操作的递归实现
    /*private void add(Node node,E e){

        if (e.equals(node.e))
            return;
        else if (e.compareTo(node.e) < 0 && node.left == null){

            node.left = new Node(e);
            size++;
            return;
        }
        else if (e.compareTo(node.e) > 0 && node.left == null){

            node.right = new Node(e);
            size++;
            return;
        }
        if (e.compareTo(node.e) < 0)
            add(node.left,e);
        else
            add(node.right,e);
    }*/
}
